/**
 * Simulation implementation
 */

#include "Simulation.h"

/////////////////////////////////////////////////

void Simulation::help()
{
	std::cout << "Tigerrad, 2006, Robby Cochran and Jay Steele\n";
	std::cout << "Clemson University\n";
	std::cout << "Escape: exit\n";
	std::cout << "Left,right arrows: decrease/increase spotlight cutoff\n";
	std::cout << "Space: toggle through combined lighting, direct only, indirect only\n";
	std::cout << "L: show view from light.  Use 0,1,2 to cycle through each render target.\n";
	std::cout << "D: show deffered shader output.  Use 0,1,2 to cycle through each render target.\n";
	std::cout << "S: show positions of secondary lights.\n";
	std::cout << "C: toggle through sampling from the light or from the camera.\n";
	std::cout << "H: toggle sampling technique.\n";
	std::cout << "A: toggle animation on/off.\n";
	std::cout << "F: toggle FPS.\n";
}

/////////////////////////////////////////////////

void Simulation::step(unsigned int delta)
{
	if (_leftKeyDown) {
		// Decrease the fov of the light 
		LightPtr l = _scene->getLight(0);
		if (l->getFov() > 0) {
			l->setFov(l->getFov() - 0.25);
		}
		std::cerr << "Light fov : " << l->getFov() << std::endl;
	}
	if (_rightKeyDown) {
		// Increase the fov of the light
		LightPtr l = _scene->getLight(0);
		if (l->getFov() < 90) {
			l->setFov(l->getFov() + 0.25);
		}
		std::cerr << "Light fov : " << l->getFov() << std::endl;
	}

	if (_animate) {
		for (unsigned int i=0; i<_scene->numEntities(); ++i) {
			EntityPtr e = _scene->getEntity(i);
			float rotSpeed = e->getRotSpeed();
			float angle = e->getRotAngle();
			angle += rotSpeed * delta/1000.0;
			if (angle > 360.0) angle -= 360.0;
			else if (angle < 0.0) angle += 360.0;
			e->setRotAngle(angle);
		}
	}
}

/////////////////////////////////////////////////

void Simulation::handleKeyPress(SDL_keysym* keysym)
{
	if (keysym->sym == SDLK_ESCAPE) {
		_finished = true;
	} else if (keysym->sym == SDLK_LEFT) {
		_leftKeyDown = true;
	} else if (keysym->sym == SDLK_RIGHT) {
		_rightKeyDown = true;
	}

}

/////////////////////////////////////////////////

void Simulation::handleKeyRelease(SDL_keysym* keysym)
{
	assert(_engine != 0);

	if (keysym->sym == SDLK_LEFT) {
		_leftKeyDown = false;
	} else if (keysym->sym == SDLK_RIGHT) {
		_rightKeyDown = false;
	} else if (keysym->sym == SDLK_SPACE) {
		_engine->toggleRadiosity();
	} else if (keysym->sym == SDLK_l) {
		_engine->toggleShowFromLight();
	} else if (keysym->sym == SDLK_0) {
		_engine->displayTex(0);
	} else if (keysym->sym == SDLK_1) {
		_engine->displayTex(1);
	} else if (keysym->sym == SDLK_2) {
		_engine->displayTex(2);
	} else if (keysym->sym == SDLK_3) {
//		_engine->displayTex(3);
	} else if (keysym->sym == SDLK_d) {
		_engine->toggleShowDS();
	} else if (keysym->sym == SDLK_s) {
		_engine->toggleShowSecLights();
	} else if (keysym->sym == SDLK_c) {
		_engine->toggleSecLightsFromCamera();
	} else if (keysym->sym == SDLK_h) {
		_engine->toggleHemisphereSampling();
	} else if (keysym->sym == SDLK_f) {
		_fps = !_fps;
	} else if (keysym->sym == SDLK_n) {
		_engine->toggleNegativeIndirectLight();
	} else if (keysym->sym == SDLK_a) {
		_animate = !_animate;
	}

}

/////////////////////////////////////////////////

void Simulation::handleMouseMotion(SDL_MouseMotionEvent* keysym)
{
}

